package com.imooc.seckill.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.imooc.commons.model.domain.ResultInfo;
import com.imooc.commons.model.pojo.SeckillVouchers;
import com.imooc.commons.utils.ResultInfoUtil;
import com.imooc.seckill.service.SeckillService;

/**
 * \* @author: bear
 * \* Date: 2021-07-29-15:47
 * \* describe:
 */
@RestController
public class SeckillController {

	@Resource
	private SeckillService seckillService;
	@Resource
	private HttpServletRequest request;

	/**
	 * 新增秒杀活动
	 *
	 * @param seckillVouchers
	 *
	 * @return
	 */
	@PostMapping("add")
	public ResultInfo<String> addSeckillVouchers(@RequestBody SeckillVouchers seckillVouchers) {
		seckillService.addSeckillVouchers(seckillVouchers);
		return ResultInfoUtil.buildSuccess(request.getServletPath(), "添加成功");
	}

	/**
	 * 秒杀下单
	 *
	 * @param voucherId
	 * @param access_token
	 *
	 * @return
	 */
	@PostMapping("{voucherId}")
	public ResultInfo<String> doSeckill(@PathVariable Integer voucherId, String access_token) {
		ResultInfo resultInfo = seckillService.doSeckill(voucherId, access_token, request.getServletPath());
		return resultInfo;
	}

	@Autowired
	private RedisTemplate redisTemplate;

	@GetMapping("test")
	public void updateRedis() {
		redisTemplate.opsForHash().put("seckill_vouchers:1", "amount", 100);
	}

}
